package com.dxjy.demo.entity;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;

import java.io.Serializable;

/**
 * 返回结果实体类
 */
@Getter
public class R implements Serializable {

    private int code;
    private String message;
    private Object data;

    private R setResult(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
        return this;
    }

    public static final int SUCCESS_CODE = ResponseCode.SUCCESS.getCode();
    public static final String SUCCESS_MESSAGE = ResponseCode.SUCCESS.getMessage();
    public static final int ERROR_CODE = ResponseCode.ERROR.getCode();
    public static final String ERROR_MESSAGE = ResponseCode.ERROR.getMessage();

    public R result(int code, String message, Object data) {
        return setResult(code, message, data);
    }

    public static R ok() {
        return new R().result(SUCCESS_CODE, SUCCESS_MESSAGE, new JSONObject());
    }

    public static R ok(Object data) {
        return new R().result(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    public static R okList(Object data) {
        JSONObject result = new JSONObject();
        result.put("list", data);
        return new R().result(SUCCESS_CODE, SUCCESS_MESSAGE, result);
    }

    public static R error() {
        return new R().result(ERROR_CODE, ERROR_MESSAGE, new JSONObject());
    }

    public static R error(ResponseCode resultEnum) {
        return new R().result(resultEnum.getCode(), resultEnum.getMessage(), new JSONObject());
    }

    public static R error(String message, Object data) {
        return new R().result(ERROR_CODE, message, data);
    }

    public static R error(int code, String message, Object data) {
        return new R().result(code, message, data);
    }

    public static R error(int code, String message) {
        return new R().result(code, message,  new JSONObject());
    }

    public static R error(String message) {
        return new R().result(ResponseCode.ERROR.getCode(), message, new JSONObject());
    }
}